import React from 'react';
import dynamic from 'dva/dynamic';

// wrapper of dynamic
const dynamicWrapper = (app, models, component) => dynamic({
  app,
  // eslint-disable-next-line no-underscore-dangle
  models: () => models.filter(m => !app._models.some(({ namespace }) => namespace === m)).map(m => import(`../models/${m}.js`)),
  // add routerData prop
  component: () => {
    const p = component();
    return new Promise((resolve, reject) => {
      p.then((Comp) => {
        resolve(props => <Comp {...props} routerData={getRouterData(app)} />);
      }).catch(err => reject(err));
    });
  },
});

const getRouterData = () =>{
    return routerData;
}

export let routerData = {
};

export const initRouterData = (app) => {
   routerData = {
    '/': {
      name:"首页",
      component: dynamicWrapper(app, ['user','loginMenu','global','login'], () => import('../layouts/BasicLayout')),

    },
    '/user': {
      component: dynamicWrapper(app, [], () => import('../layouts/UserLayout')),
    },
    '/user/login': {
      component: dynamicWrapper(app, ['login','loginMenu'], () => import('../routes/Login/Login')),
    },
    '/system/user': {
      component: dynamicWrapper(app, ['user','role'], () => import('../routes/System/UserManager')),
    },
    '/system/role': {
      component: dynamicWrapper(app, ['role','menu'], () => import('../routes/System/RoleManager')),
    },
    '/system/menu': {
      component: dynamicWrapper(app, ['menu'], () => import('../routes/System/MenuManager')),
    },
    '/exception/403': {
      component: dynamicWrapper(app, [], () => import('../routes/Exception/403')),
    },
    '/exception/404': {
      component: dynamicWrapper(app, [], () => import('../routes/Exception/404')),
    },
    '/exception/500': {
      component: dynamicWrapper(app, [], () => import('../routes/Exception/500')),
    },
  };
  return routerData;
};
